<!--
 * @Description: 手写菜品功能组件
 * @Author: AI Assistant
 * @Date: 2025-01-27
-->
<template>
  <custom-dialog v-model:visible="dialogVisible">
    <div class="dialog-content">
      <div class="title">手写菜品</div>

      <!-- 菜品分类选择 -->
      <div class="category-section">
        <div class="category-list" v-loading="categoryLoading">
          <div
            class="category-item"
            :class="{ 'category-item-active': cateActiveIndex === index }"
            v-for="(item, index) in cateList"
            :key="item.id + index"
            @click="handleChangeTab(index, item)"
          >
            {{ item.name }}
          </div>
        </div>
      </div>

      <!-- 菜品信息输入 -->
      <div class="form-section">
        <div class="form-item">
          <div class="label">菜品名称</div>
          <div class="input-container">
            <input
              class="input"
              type="text"
              placeholder="请先选择上方菜品分类，再输入此菜品名"
              v-model.trim="formData.write_food_name"
            />
            <img
              v-if="formData.write_food_name"
              class="clear-icon"
              @click="handleClearInput(1)"
              src="@/assets/images/home/input-clear.png"
              alt=""
            />
          </div>
        </div>

        <div class="form-item">
          <div class="label">菜品单价</div>
          <div class="input-container">
            <input
              class="input"
              type="number"
              placeholder="请输入菜品价格"
              v-model.trim="formData.write_food_price"
              :min="0"
            />
            <img
              v-if="formData.write_food_price"
              class="clear-icon"
              @click="handleClearInput(2)"
              src="@/assets/images/home/input-clear.png"
              alt=""
            />
          </div>
        </div>
      </div>

      <!-- 按钮区域 -->
      <div class="btn-box">
        <el-button class="cancel-btn" @click="onCancel">取消</el-button>
        <el-button class="confirm-btn" @click="onConfirm" :loading="loading">确定</el-button>
      </div>
    </div>
  </custom-dialog>
</template>

<script setup lang="ts">
import { ref, computed, watch } from 'vue'
import { ElMessage } from 'element-plus'
import { writeFoodList } from '@/api/food'
import CustomDialog from '@/components/CustomDialog/Index.vue'

interface Props {
  modelValue: boolean
}

interface Emits {
  (e: 'update:modelValue', value: boolean): void
  (e: 'confirm', data: any): void
}

const props = defineProps<Props>()
const emit = defineEmits<Emits>()

// 弹窗显示状态
const dialogVisible = computed({
  get: () => props.modelValue,
  set: val => emit('update:modelValue', val)
})

// 数据状态
const categoryLoading = ref(false)
const loading = ref(false)
const cateList = ref<any[]>([])
const cateActiveIndex = ref(-1)
const formData = ref({
  write_food_name: '', // 手写菜品名称
  write_food_price: '', // 手写菜品价格
  category_id: '', // 手写菜品选择的分类id
  food_sku_id: '' // 手写菜品的 菜品food_sku_id
})

// 监听弹窗显示状态
watch(dialogVisible, val => {
  if (val) {
    getCategoryList()
    resetFormData()
  }
})

// 获取菜品分类列表
const getCategoryList = async () => {
  try {
    categoryLoading.value = true
    const params = {
      parent_id: 0
    }
    const res = await writeFoodList()
    if (res.status === 200) {
      cateList.value = res.data || []
    } else {
      ElMessage.error('获取菜品分类失败')
    }
  } catch (error) {
    console.error('获取菜品分类失败:', error)
    ElMessage.error('获取菜品分类失败')
  } finally {
    categoryLoading.value = false
  }
}

// 清空输入框
const handleClearInput = (type: number) => {
  if (type === 1) {
    formData.value.write_food_name = ''
  } else {
    formData.value.write_food_price = ''
  }
}

// 切换选中对应的分类
const handleChangeTab = (index: number, item: any) => {
  cateActiveIndex.value = index
  formData.value.category_id = item.id
  formData.value.food_sku_id = item.skuList[0].id
}

// 确认提交事件
const onConfirm = async () => {
  if (!formData.value.category_id) {
    ElMessage.warning('请先选择菜品分类')
    return
  }
  if (!formData.value.write_food_name) {
    ElMessage.warning('请输入菜品名称')
    return
  }

  try {
    loading.value = true

    // 构建手写菜品数据
    const handwriteFoodData = {
      id: 0, // 是手写菜品food_id默认值是0
      is_write_food: 1, // 0:不是手写菜品  1：是手写菜品
      write_food_name: formData.value.write_food_name, // 手写菜品名称
      write_food_price: formData.value.write_food_price || '0', // 手写菜品价格
      category_id: formData.value.category_id, // 手写菜品选择的分类id
      food_sku_id: formData.value.food_sku_id,
      foodNum: 1, // 数量
      discount: 10, // 菜品单独折扣1-10   10为不打折
      isUseSingleFoodDiscount: false, // 判断是否使用了单菜折扣
      // 这些都是展示专用，下单的时候再重新组装数据即可
      foodName: formData.value.write_food_name, // 菜品名称
      foodPrice: formData.value.write_food_price, // 菜品原价
      foodTotalPrice: formData.value.write_food_price, // 菜品总价
      foodUnit: '份' // 菜品单位
      
    }

    // 发送确认事件
    emit('confirm', handwriteFoodData)

    // 关闭弹窗
    dialogVisible.value = false
    resetFormData()
  } catch (error) {
    console.error('手写菜品添加失败:', error)
    ElMessage.error('手写菜品添加失败')
  } finally {
    loading.value = false
  }
}

// 取消
const onCancel = () => {
  dialogVisible.value = false
  resetFormData()
}

// 重置数据
const resetFormData = () => {
  cateActiveIndex.value = -1
  formData.value = {
    write_food_name: '',
    write_food_price: '',
    category_id: '',
    food_sku_id: ''
  }
}
</script>

<style lang="scss" scoped>
.dialog-content {
  //   padding: 20px;
  min-width: 400px;

  .title {
    font-size: 18px;
    font-weight: 700;
    color: #333333;
    text-align: center;
    margin-bottom: 30px;
  }

  .category-section {
    margin-bottom: 30px;

    .category-list {
      display: flex;
      flex-wrap: wrap;
      gap: 14px;
      padding: 20px;
      min-height: 200px;
      background: #f4f4f4;
      border-radius: 12px;
      max-height: 200px;
      overflow-y: auto;

      .category-item {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 120px;
        height: 48px;
        background-color: #ffffff;
        border: 1px solid #d2d2d2;
        border-radius: 12px;
        font-size: 14px;
        color: #333333;
        cursor: pointer;
        transition: all 0.3s;

        &:hover {
          border-color: #7c0a03;
          color: #7c0a03;
        }

        &.category-item-active {
          border-color: #7c0a03;
          color: #7c0a03;
          background-color: #fff5f5;
        }
      }
    }
  }

  .form-section {
    margin-bottom: 30px;

    .form-item {
      display: flex;
      align-items: center;
      margin-bottom: 20px;

      .label {
        width: 100px;
        font-size: 16px;
        color: #333333;
        margin-right: 17px;
      }

      .input-container {
        flex: 1;
        display: flex;
        align-items: center;
        justify-content: space-between;
        height: 48px;
        padding: 12px 18px;
        background-color: #ffffff;
        border: 1px solid #d2d2d2;
        border-radius: 12px;
        box-sizing: border-box;

        .input {
          flex: 1;
          outline: none;
          border: none;
          font-size: 14px;
          color: #333333;
          background: transparent;

          &::placeholder {
            color: #999999;
          }
        }

        .clear-icon {
          width: 20px;
          height: 20px;
          cursor: pointer;
          border-radius: 50%;
        }
      }
    }
  }

  .btn-box {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding-top: 20px;

    .cancel-btn {
      width: 190px;
      height: 52px;
      border: 1px solid #d2d2d2;
      border-radius: 12px;
      font-size: 18px;
      color: #353535;
      background: #ffffff;

      &:hover {
        border-color: #7c0a03;
        color: #7c0a03;
      }
    }

    .confirm-btn {
      width: 190px;
      height: 52px;
      background: #7c0a03;
      border-radius: 12px;
      font-size: 18px;
      color: #ffffff;
      border: none;

      &:hover {
        background: #5a0802;
      }
    }
  }
}

// 滚动条样式
.category-list::-webkit-scrollbar {
  width: 6px;
}

.category-list::-webkit-scrollbar-thumb {
  background: #d2d2d2;
  border-radius: 3px;
}

.category-list::-webkit-scrollbar-track {
  display: none;
}
</style>
